# encoding: utf-8
import xlrd
import sys
import os
reload(sys)
sys.setdefaultencoding("utf-8")

#print os.path.join(os.path.dirname(__file__),u'装备表.xls').replace("\\","/")
database = xlrd.open_workbook(os.path.join(os.path.dirname(__file__),u'装备表.xls').replace("\\","/"))


def getGunid(nameofGun,type='gun'): 
	if type == 'gun':
		sheet = database.sheet_by_name(u'枪支表')
	elif type == 'ThrowExplosive':
		sheet = database.sheet_by_name(u'投掷类爆炸物')
	elif type == 'Metee':
		sheet = database.sheet_by_name(u'冷兵器表')
	elif type == 'inventory':
		sheet = database.sheet_by_name(u'其它道具表')

	rows = sheet.nrows
	cols =  sheet.ncols
	allGunNames = sheet.col_values(3)[1:]
	allGunIds = sheet.col_values(2)[1:]

	result = []

	for i in range(0,rows-1):
		name = str(allGunNames[i])
		if (nameofGun in name) or(nameofGun.lower() in name) or (nameofGun.upper() in name) or (nameofGun.title() in name) or (nameofGun.capitalize() in name):
			value = {}
			value['item'] = name
			value['ID'] = (int)(allGunIds[i])
			result.append(value)

	return result
	
def getRoleid(nameofRole,type='person'):
	roles = xlrd.open_workbook(os.path.join(os.path.dirname(__file__),u'角色表.xls').replace("\\","/"))
	if type == 'person': 
		sheet = roles.sheet_by_name(u'普通角色属性表')
		allnames = sheet.col_values(3)[1:]
		allIds = sheet.col_values(2)[1:]
	elif type == 'monster':
		sheet = roles.sheet_by_name(u'副本难度怪物加成表')
		allnames = sheet.col_values(2)[1:]
		allIds = sheet.col_values(1)[1:]
	else:
		return []

	rows = sheet.nrows
	cols =  sheet.ncols
	

	result = []

	for i in range(0,rows-1):
		name = str(allnames[i])
		if (nameofRole in name) or (nameofRole.lower() in name) or (nameofRole.upper() in name) or (nameofRole.title() in name) or (nameofRole.capitalize() in name):
			value = {}
			value['item'] = name
			value['ID'] = (int)(allIds[i])
			result.append(value)

	return result


if __name__ == '__main__':
	d =  getGunid('a','gun')
	for k in d:
		#print k, d[k]
		for i in d[k]:
			print i